package servlets;

import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AServlet
 */
public class AServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void forwardOnError(HttpServletRequest request, HttpServletResponse response, String sessionParam, String errorMessage, String redirectLink){
		request.getSession(true).setAttribute(sessionParam, errorMessage);
		RequestDispatcher rd = getServletConfig().getServletContext().getRequestDispatcher(redirectLink);
		try {
			rd.forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			//Nothing to do here
		} 
		return;
	}
	
	public static void finishTransaction(Connection con, boolean commit){
		if (commit){
			try {
				con.commit();
			} catch (SQLException e) {
				try {
					con.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				e.printStackTrace();
			}
		}
		else{
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
	}
}
